package petstore.web.servlet.cart;

import com.alibaba.fastjson.JSON;
import jdk.nashorn.internal.runtime.Undefined;
import petstore.persistence.CartDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/cartNumber")
public class CartNumber extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("into");
        HttpSession session  = req.getSession();
        Object uid=session.getAttribute("uid");
        System.out.println((int)uid);
        if(uid==null)
        {
            resp.setContentType("text/plain");
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().print("0");
            return;
        }

        CartDaoImpl cartDao = new CartDaoImpl((int)uid);
        int numbers = cartDao.getCartItemList().size();
        System.out.println("数量："+numbers);
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().print(numbers);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
